package com.lyf.juc.learn.sggadvance.future.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

/**
 * @author LYF
 * @date 2022/7/1
 */
public class CompletableFutureDemo {

    static NetMallService netMallService = new NetMallServiceImpl();

    static List<String> getPrice(List<String> netMalls,String bookName){
           return netMalls.stream()
                   .map(netMall->String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName)))
                   .collect(Collectors.toList());
    }

    static List<String> getPriceByAsync(List<String> netMalls,String bookName){
        /// 同step by step几乎一致
//        return netMalls.stream()
//                .map(netMall->
//                        //String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName)))
//                        CompletableFuture.supplyAsync(()-> String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName))).join()
//                )
//                .collect(Collectors.toList());

        /// 同step by step几乎一致
//        return netMalls.stream()
//                .map(
//                        //String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName)))
//                        // CompletableFuture.supplyAsync(()-> String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName))).join()
//                        netMall->CompletableFuture.supplyAsync(()-> String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName)))
//
//                )
//                .map(stringCompletableFuture -> stringCompletableFuture.join())
//                .collect(Collectors.toList());

        // 提升
        return netMalls.stream()
                .map(
                        //String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName)))
                        // CompletableFuture.supplyAsync(()-> String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName))).join()
                        netMall->CompletableFuture.supplyAsync(()-> String.format(bookName+" at "+netMall+" in price::%f" ,netMallService.getPrice(netMall,bookName)))
                )
                // 需要先进行收集起来 TODO---重点理解的地方
                .collect(Collectors.toList())
                .stream()

                .map(stringCompletableFuture -> stringCompletableFuture.join())
                .collect(Collectors.toList());
    }


    static private List<String> malls = Arrays.asList("pdd","tmall","ddd");

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        List<String> list = getPrice(malls,"mysql");
        long end = System.currentTimeMillis();

        System.out.println("耗时::"+(end-start));
        list.stream().forEach(u->{
            System.out.println(u);
        });

        long start2 = System.currentTimeMillis();
        List<String> list2 = getPriceByAsync(malls,"mysql");
        long end2 = System.currentTimeMillis();

        System.out.println("耗时::"+(end2-start2));
        list2.stream().forEach(u->{
            System.out.println(u);
        });

//        ForkJoinPool forkJoinPool= new ForkJoinPool();
//        forkJoinPool.submit()


    }

}
